{include file='public/_header' /}

<!-- Main Container -->
<div class="main-container container-fluid">
    <!-- Page Container -->
    <div class="page-container">

        <!-- Page Sidebar -->
        {include file='public/_left' /}
        <!-- /Page Sidebar -->
        <!-- Page Content -->
        <div class="page-content">
            <!-- Page Breadcrumb -->
            <div class="page-breadcrumbs">
                <ul class="breadcrumb">
                    <li class="active">
                        <i class="fa fa-home"></i>&nbsp;管理员管理
                    </li>
                    <li>管理员列表</li>
                </ul>
            </div>
            <!-- /Page Breadcrumb -->
            <!-- Page Body -->
            <div class="page-body">
                <a href="{:url('admin/admin/adminadd')}" class="btn btn-sm btn-primary"><i class="fa fa-plus"></i>&nbsp;管理员添加</a>
                <div class="row">
                    <div class="col-xs-12">
                        <div class="widget">
                            <div class="widget-header">
                                <span class="widget-caption">管理员列表</span>
                                <div class="widget-buttons">
                                    {$admins|raw|replace_str}

                                </div>
                            </div>
                            <div class="widget-body">
                                <table class="table table-hover table-bordered">
                                    <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>用户名</th>
                                            <th>昵称</th>
                                            <th>邮箱</th>
                                            <th>状态</th>
                                            <th>是否超级管理员</th>
                                            <th>创建时间</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {volist name="admins" id="admin"}
                                            <tr>
                                                <td>{$admin.id}</td>
                                                <td>{$admin.username}</td>
                                                <td>{$admin.nickname}</td>
                                                <td>{$admin.email}</td>
                                                <td>{if $admin.status == 1} 启用 {else /} 禁用 {/if}</td>
                                                <td>{if $admin.is_super == 1} 是 {else /} 否 {/if}</td>
                                                <td>{$admin.create_time}</td>
                                                <td>
                                                    <!-- {:url('admin/admin/adminedit', ['id' => $admin.id])} -->
                                                    <!-- 判断状态 -->
                                                    {if session('admin.is_super') == 1}
                                                        {if $admin.username == session('admin.username')}
                                                            <a class="btn btn-xs btn-darkorange" disabled="true">禁用</a>
                                                        {elseif $admin.status == 1 /}
                                                            <a href="#" class="btn btn-xs btn-darkorange disableAdmin" dataid="{$admin.id}">禁用</a>
                                                        {else /}
                                                            <a href="#" class="btn btn-xs btn-success useAdmin" dataid="{$admin.id}">启用</a>
                                                        {/if}
                                                    {/if}

                                                    <!-- 判断是否超级管理员 -->
                                                    {if session('admin.is_super') == 1}
                                                        {if $admin.username == session('admin.username')}
                                                            <a class="btn btn-darkorange btn-xs" disabled="true">取消授权</a>
                                                        {elseif $admin.is_super == 1 /}
                                                            <a href="#" class="btn btn-primary btn-xs cancelGrantAuthority" dataid="{$admin.id}">取消授权</a>
                                                        {else /}
                                                            <a href="#"  class="btn btn-darkorange btn-xs grantAuthority" dataid="{$admin.id}">授权</a>
                                                        {/if}
                                                    {/if}

                                                    <!-- 超级管理员才能编辑 -->
                                                    {if session('admin.is_super') == 1}
                                                        <a href="{:url('admin/admin/adminedit', ['id' => $admin.id])}" class="btn btn-azure btn-xs">编辑</a>
                                                    {else /}
                                                        <a class="btn btn-darkorange btn-xs" disabled="true">编辑</a>
                                                    {/if}

                                                    <!-- 超级管理员才能删除 -->
                                                    {if session('admin.is_super') == 1}
                                                        {if $admin.username == session('admin.username')}
                                                            <a class="btn btn-danger btn-xs deladmin" disabled="true">删除</a>
                                                        {else /}
                                                            <a href="#" class="btn btn-danger btn-xs deladmin" dataid="{$admin.id}">删除</a>
                                                        {/if}
                                                    {/if}


                                                </td>
                                            </tr>
                                        {/volist}

                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- /Page Body -->
        </div>
        <!-- /Page Content -->

    </div>
    <!-- /Page Container -->
    <!-- Main Container -->

</div>

{include file='public/_js' /}

<script>
    $(function () {
        $(".pagination").addClass('pagination-sm');
    });

    // 禁用
    $(".disableAdmin").click(function () {
        var id = $(this).attr('dataid');
        layer.confirm('确定禁用该用户吗?',{
            icon: 3,
            title: '提示'
        }, function (index) {
            layer.close(index);
            $.ajax({
                url: "{:url('admin/admin/disableadmin')}",
                type: "post",
                data: {id: id},
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    if (data.code == 1) {
                        layer.msg("已禁用", {
                            icon: 6,
                            time: 500
                        }, function () {
                            location.reload();
                        });
                    } else {
                        layer.open({
                            title: '操作失败',
                            content: data.msg,
                            icon: 5,
                            anim: 6
                        });
                    }
                }
            });
        });
        return false;
    });

    // 启用
    $(".useAdmin").click(function () {
        var id = $(this).attr('dataid');
        layer.confirm('确定启用该用户吗?',{
            icon: 3,
            title: '提示'
        }, function (index) {
            layer.close(index);
            $.ajax({
                url: "{:url('admin/admin/useadmin')}",
                type: "post",
                data: {id: id},
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    if (data.code == 1) {
                        layer.msg("已启用", {
                            icon: 6,
                            time: 500
                        }, function () {
                            location.reload();
                        });
                    } else {
                        layer.open({
                            title: '操作失败',
                            content: data.msg,
                            icon: 5,
                            anim: 6
                        });
                    }
                }
            });
        });
        return false;
    });

    // 授权
    $(".grantAuthority").click(function () {
        var id = $(this).attr('dataid');
        layer.confirm('确定授权该用户吗?',{
            icon: 3,
            title: '提示'
        }, function (index) {
            layer.close(index);
            $.ajax({
                url: "{:url('admin/admin/grantauthority')}",
                type: "post",
                data: {id: id},
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    if (data.code == 1) {
                        layer.msg("已授权", {
                            icon: 6,
                            time: 500
                        }, function () {
                            location.reload();
                        });
                    } else {
                        layer.open({
                            title: '操作失败',
                            content: data.msg,
                            icon: 5,
                            anim: 6
                        });
                    }
                }
            });
        });
        return false;
    });

    // 取消授权
    $(".cancelGrantAuthority").click(function () {
        var id = $(this).attr('dataid');
        layer.confirm('确定取消授权吗?',{
            icon: 3,
            title: '提示'
        }, function (index) {
            layer.close(index);
            $.ajax({
                url: "{:url('admin/admin/cancelGrantAuthority')}",
                type: "post",
                data: {id: id},
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    if (data.code == 1) {
                        layer.msg("已取消授权", {
                            icon: 6,
                            time: 500
                        }, function () {
                            location.reload();
                        });
                    } else {
                        layer.open({
                            title: '操作失败',
                            content: data.msg,
                            icon: 5,
                            anim: 6
                        });
                    }
                }
            });
        });
        return false;
    });

    // 删除管理员
    $(".deladmin").click(function () {
        var id = $(this).attr('dataid');
        layer.confirm('确定删除吗?',{
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                $.ajax({
                    url: "{:url('admin/admin/admindel')}",
                    type: "post",
                    data: {id: id},
                    dataType: 'json',
                    success: function (data) {
                        console.log(data);
                        if (data.code == 1) {
                            layer.msg("删除成功", {
                                icon: 6,
                                time: 500
                            }, function () {
                                location.href = data.url;
                            });
                        } else {
                            layer.open({
                                title: '删除失败',
                                content: data.msg,
                                icon: 5,
                                anim: 6
                            });
                        }
                    }
                });
         });


        return false;
    });

</script>

</body>
<!--  /Body -->
</html>
